/**
 * Created by Administrator on 2016/11/17.
 */



(function(){
    var forget={
        init:function(){
            this.validate();
            this.sendsms();
        },
        validate:function(){

            /* 输入账户名*/
            if($(".forget1").length>0){
                $(".forget1").on("click",".btn",function(){
                    if($("input[name=mobile]").val()==""){
                        layer.msg('手机号码不能为空',{icon:2,time:1000});
                        return false;
                    }else if(!/^1\d{10}$/.test($("input[name=mobile]").val())){
                        layer.msg('手机号码输入有误',{icon:2,time:1000});
                        $("input[name=mobile]").val("");
                        return false;
                    }
                });
            }

            /*验证身份*/
            if($(".forget2").length>0){
                $(".forget2").on("click",".btn",function(){
                    if($("input[name='code']").val()==""){
                        layer.msg('验证码不能为空',{icon:2,time:1000});
                        return false;
                    }
                });
            }

            /*重置密码*/
            if($(".forget3").length>0){

                $(".forget3").on("click",".btn",function(){

                    var password=$("input[name='password']");

                    if(password.val()==""){
                        layer.msg('新密码不能为空',{icon:2,time:1000});
                        return false;
                    }else if(!/^\w{6,15}$/.test(password.val())){
                        layer.msg('新密码输入有误',{icon:2,time:1000});
                        return false;
                    }

                    if(password.val()!=$("input[name='password2']").val()){
                        layer.msg('确认密码不一致',{icon:2,time:1000});
                        return false;
                    }
                });
            }
        },
        sendsms:function(){
        	$(".forget2").on("click", ".sendsms", function(){
        		var this_a = $(".forget2").find('.s_code');
        		var num = 60;
        		var t;
        		var second = function(){
        			this_a.html(num +'秒后可重发');
        			t = setTimeout(second,1000);
        			if(num === 0){
        				clearTimeout(t);this_a.addClass('sendsms');this_a.html('获取短信验证码');
        			}
        			num --;
        		}
        		var mobile = $("input[name=mobile]");
        		if(mobile.val()){
                	this_a.removeClass('sendsms');
                    $.post(get_loginUrl()+'Send_sms/index', {mobile:mobile.val()}, function(json){
                        if(json == false) {
                        	this_a.addClass('sendsms');
                            layer.msg('短信发送失败',{icon:2,time:1000});
                        }else{
                        	second();
                            layer.msg('短信发送成功',{icon:1,time:1000});
                            setTimeout(function(){this_a.addClass('sendsms');},60*1000);
                        }
                    },'json');
                }else{
                    layer.msg('请填写手机号',{icon:2,time:1000});
                }
        	});
        },
    };
    forget.init();
})();




